<?php
/**
 * MVC Controller base class, used to manage the different sections of the application ( aka Controllers )
 *
 * @author Highstrike (highstrike@gmail.com)
 * @author Wavetrex (wavetrex@gmail.com)
 * @package CargoClaims
 * @version 1.0.dev
 */
abstract class MVC_Ctrl
{
	/**
	 * Contains the Drytemplate object that will render the template + $this-> output into the final HTML
	 * @var Drytemplate
	 */
	protected $_drytpl;

	/**
	 * The output data which is being sent to the template system
	 * @var array
	 */
	protected $_out = array();

	/**
	 * The session object (always present)
	 * @var MVC_Persist
	 */
	protected $_session;

// ===== System methods, should not be called from inside the controller ===== //

	/**
	 * Controller constructor, sets up some things like Session, Language and View ( Template )
	 */
	public function __construct()
	{
		$this->_session = new MVC_Persist('session');

		// set language
		if(!isset($this->_session-> language))
			$this->_session-> language = MVC_Core::$settings['core']['default_language'];
		MVC_NLS::set_Language($this->_session-> language);

		// sets up the DRYTemplate object that will be used to render the output
		$CFG = new stdClass();
		$CFG-> paths = array('templates'=> array(
			ROOT. DS. 'app'. DS. 'view'
		));
		$this->_drytpl = new Drytemplate($CFG);
		$this->_out = array();

		// give the templates access to the session ( which contains user information, current company )
		$this->_out['session'] = &$this->_session;

		// some always present information which go into the output variable
		$this->_out['base_url'] = MVC_Core::$settings['core']['base_url'];
		$this->_out['title'] = MVC_Core::$settings['html']['default_title'];
	}

	/**
	 * Quick setting of output content
	 *
	 * @param string $var
	 * @param mixed $value
	 */
	public function __set($var, $value)
	{
		$this->_out[$var] = $value;
	}

	/**
	 * Dispatches the action (which usually comes from a request parameter) to the Action methods which are inside the Controller
	 *
	 * @param string $action
	 */
	public function _dispatch($action = null)
	{
		// the controller output is managed, and display only happens only after it has completed successfully
		ob_start();
		if(is_string($action)) {
			if(method_exists($this, 'action_'.$action))
				call_user_func(array($this, 'action_'.$action));
			else {
				$ctrl = explode('_', get_class($this));
				throw new MVC_Exception("Action &quot;{$action}&quot; unavailable in controller: ". end($ctrl));
			}
		} else
			// if an action hasn't been specified, just run the "index"
			$this-> action_Index();
		// in case of an Exception, we don't reach this point.
		ob_end_flush();
	}

// ===== Methods used in all the controllers (since they basically load much of the same things) ===== //

	/**
	 * Displays the view for this controller
	 *
	 * @param string $view_file
	 */
	public function view($view_file)
	{
		echo $this->_drytpl-> render_File($view_file, $this->_out);
	}

	/**
	 * Redirects the browser to another page ( usually used after a POST or if the user doesn't have access to a page )
	 *
	 * @param string $where Redirect location
	 */
	public function redirect($where)
	{
		ob_end_clean();
		header('Location: '.$where);
		die;
	}

	/**
	 * Return as json response
	 *
	 * @param mixed $what What to encode as json
	 */
	public function json($what)
	{
		header('Content-type: application/json');
		die(json_encode($what));
	}

// ===== The default action(s) ===== //

	/**
	 * Does nothing, this is the empty index() action that must exist for when an action is not specified and the controller doesn't an Index action
	 */
	public function action_Index()
	{
	}

}